package com.miniso.robot.bean;

import ch.qos.logback.core.util.CloseUtil;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.digest.MD5;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.logging.log4j.util.Base64Util;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author guojunwang
 * @date 2021-10-18 20:49
 */
@Getter
@NoArgsConstructor
public class ImageMessage extends Message{

    private final String msgtype = "image";

    private Map<String,String> image = new HashMap(8);

    public void writeImage(InputStream inputStream) {
        byte[] bytes = IoUtil.readBytes(inputStream);

        try {
            image.put("base64",Base64Encoder.encode(bytes));
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(bytes);
            BigInteger bi = new BigInteger(1, md5.digest());
            image.put( "md5",bi.toString(16) );
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }finally {
            IoUtil.close(inputStream);
        }
    }

    public void setImageInfo(String base64,String md5) {
        image.put("base64",base64);
        image.put( "md5",md5 );
    }

}
